package longrise.phone.com.bjjt_jyb.compensate;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import java.text.ParseException;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.AllCapTransformationMethod;
import longrise.phone.com.bjjt_jyb.Utils.CheckCard;
import longrise.phone.com.bjjt_jyb.Utils.StringUtils;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.Utils.UiUtils;


/**
 * 事故上传的信息填写界面，Created by Administrator on 2016/5/12.
 */
public class AccidentLoadingViewNewTwo extends LFView implements View.OnClickListener,  ILFMsgListener {
    private String appCaseNo;//案件编号
    private Context context = null;
    private View view = null;
    private Button bt_accidentloading_new_two;
    private EditText et_accidentloadingpic1_new_two;
    private EditText et_accidentloadingpic3_new_two;
    private EditText et_accidentloadingpic5_new_two;
    private BaseApplication mApplication;
    private Dialog processDialog;
    private RadioGroup rg_accident1_new_two;
    private String dfCarType;
    private String bfCarType;
    private LinearLayout hn_btn_back;
    private String accidentType;
    private String discription;
    private EditText et_accdentidcardhe_new_two;
    private EditText et_accdentidcardme_new_two;
    private RadioGroup rg_accidenttwo_new_two;
    private EditText et_accidentloadingpictwo_new_two;
    private EditText et_accidentcarnum1_new_two;
    private EditText et_accidentcarnum2_new_two;
    private EditText et_wz1;
    private String nationid;
    private String imageaddress;
    private boolean test=false;//测试内网时置为true；

    public AccidentLoadingViewNewTwo(Context context, String appCaseNo, String accidentType, String discription) {
        super(context);
        this.context = context;
        this.appCaseNo = appCaseNo;
        this.mApplication = (BaseApplication) context.getApplicationContext();
        this.accidentType = accidentType;
        this.discription = discription;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_accidentloadingnewtwo, null);
        bt_accidentloading_new_two = (Button) view.findViewById(R.id.bt_accidentloading_new_two);
        rg_accident1_new_two = (RadioGroup) view.findViewById(R.id.rg_accident1_new_two);
        rg_accidenttwo_new_two = (RadioGroup) view.findViewById(R.id.rg_accidenttwo_new_two);
        et_accidentloadingpic1_new_two = (EditText) view.findViewById(R.id.et_accidentloadingpic1_new_two);
        et_accidentloadingpic3_new_two = (EditText) view.findViewById(R.id.et_accidentloadingpic3_new_two);
        et_accidentloadingpic5_new_two = (EditText) view.findViewById(R.id.et_accidentloadingpic5_new_two);
        et_accidentcarnum1_new_two = (EditText) view.findViewById(R.id.et_accidentcarnum1_new_two);
        et_accidentcarnum2_new_two = (EditText) view.findViewById(R.id.et_accidentcarnum2_new_two);
        et_accidentloadingpictwo_new_two = (EditText) view.findViewById(R.id.et_accidentloadingpictwo_new_two);
        et_accdentidcardhe_new_two = (EditText) view.findViewById(R.id.et_accdentidcardhe_new_two);//对方驾驶证号
        et_accdentidcardme_new_two = (EditText) view.findViewById(R.id.et_accdentidcardme_new_two);//本方驾驶证号
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        LinearLayout ll_sgdd1_new_two = (LinearLayout) view.findViewById(R.id.ll_sgdd1_new_two);//事故地点
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("事故上传");
        //监测大写
        et_accidentcarnum1_new_two.setTransformationMethod(new AllCapTransformationMethod());
        et_accdentidcardhe_new_two.setTransformationMethod(new AllCapTransformationMethod());
        et_accidentcarnum2_new_two.setTransformationMethod(new AllCapTransformationMethod());
        et_accdentidcardme_new_two.setTransformationMethod(new AllCapTransformationMethod());
        et_wz1 = (EditText) view.findViewById(R.id.et_wz1_new_two);//事故地点
        /**设置高速交警**/
        nationid = mApplication.getNationid();
        if(null!= nationid &&"1".equals(nationid)){
            ll_sgdd1_new_two.setVisibility(View.VISIBLE);
        }else {
            ll_sgdd1_new_two.setVisibility(View.GONE);
        }
        pictureRecognitionData();
        //测试时打开标记，填充布局
        if(test){
            et_accidentloadingpic1_new_two.setText("甲a");
            et_accidentcarnum1_new_two.setText("WBAKR0103E0J27794");
            et_accidentloadingpic3_new_two.setText("13554042403");
            et_accdentidcardhe_new_two.setText("421123199012210831");
            et_accidentloadingpic5_new_two.setText("乙a");
            et_accidentcarnum2_new_two.setText("LB37102S9BH009039");
            et_accidentloadingpictwo_new_two.setText("15527051575");
            et_accdentidcardme_new_two.setText("410101199601010159");
        }
    }

    private void regEvent(boolean b) {
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        if (null != bt_accidentloading_new_two) {
            bt_accidentloading_new_two.setOnClickListener(b ? this : null);
        }
        if (null != rg_accident1_new_two) {
            rg_accident1_new_two.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_accident_1_new_two:
                            dfCarType = "3";
                            break;
                        case R.id.rb_accident_2_new_two:
                            dfCarType = "4";
                            break;
                        case R.id.rb_accident_3_new_two:
                            dfCarType = "5";
                            break;
                    }
                }
            });
        }
        if (null != rg_accidenttwo_new_two) {
            rg_accidenttwo_new_two.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_accident_two_1_new_two:
                            bfCarType = "3";
                            break;
                        case R.id.rb_accident_two_2_new_two:
                            bfCarType = "4";
                            break;
                        case R.id.rb_accident_two_3_new_two:
                            bfCarType = "5";
                            break;
                    }
                }
            });
        }
        if (b) {
            this.addILFMsgListener(this);
        } else {
            this.removeILFMsgListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        String carNoJ = et_accidentcarnum1_new_two.getText().toString().trim();//甲方车架号
        String carNoY = et_accidentcarnum2_new_two.getText().toString().trim();//乙方车架号
        switch (v.getId()) {
            case R.id.hn_btn_back:
                showHomeView();
                break;

            case R.id.bt_accidentloading_new_two:
                String phoneHis = et_accidentloadingpic3_new_two.getText().toString().trim();//甲方手机号
                String IDCardJ = et_accdentidcardhe_new_two.getText().toString().trim();//甲方驾驶证号
                String phoneY = et_accidentloadingpictwo_new_two.getText().toString().trim();//乙方手机号
                String IDCardY = et_accdentidcardme_new_two.getText().toString().trim();//乙方驾驶证号
                if (!ifEdittextNull()) {
                    return;
                }
                if (!StringUtils.isPhone(phoneHis)) {
                    UiUtil.showToast(context, "甲方联系电话错误");
                    return;
                }
                try {
                    if (!CheckCard.IDCardValidate(IDCardJ)) {
                        UiUtil.showToast(context, "甲方身份证号错误");
                        return;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if(null!=nationid&&"1".equals(nationid)){
                    String etwz = et_wz1.getText().toString().trim();//高速交警判断地点为空
                    if (StringUtils.isEmpty(etwz)) {
                        UiUtil.showToast(context, "请填写事故地点");
                        return;
                    }
                }
                if (!StringUtils.isPhone(phoneY)) {
                    UiUtil.showToast(context, "乙方联系电话错误");
                    return;
                }
                try {
                    if (!CheckCard.IDCardValidate(IDCardY)) {
                        UiUtil.showToast(context, "乙方身份证号错误");
                        return;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //判断双方的车架号是否相同
                if (carNoJ.equals(carNoY)) {
                    UiUtil.showToast(context, "事故双方车架号相同");
                    return;
                }
                //判断双方的手机号是否相同
                if (phoneY.equals(phoneHis)) {
                    UiUtil.showToast(context, "事故双方联系电话相同");
                    return;
                }
                //判断双方的车架号是否相同
                if (IDCardJ.equals(IDCardY)) {
                    UiUtil.showToast(context, "事故双方驾驶证号相同");
                    return;
                }
                submit();
                break;
        }
    }

    //创造双方的bean跳转,这是责任认定书的bean[]
    private void submit() {
        EntityBean hisBean = new EntityBean();
        hisBean.set("carType", dfCarType);
        hisBean.set("carownname", et_accidentloadingpic1_new_two.getText().toString().trim());
        hisBean.set("casecarphone", et_accidentloadingpic3_new_two.getText().toString().trim());
        hisBean.set("casecarId", et_accdentidcardhe_new_two.getText().toString().trim().toUpperCase());
        hisBean.set("frameno", et_accidentcarnum1_new_two.getText().toString().trim().toUpperCase());
        if(null!=nationid&&"1".equals(nationid)){
            String trim = et_wz1.getText().toString().trim();
            hisBean.set("accidentplace", trim);//事故地点
            mApplication.setAddress(trim);
        }
        EntityBean myBean = new EntityBean();
        myBean.set("carownname", et_accidentloadingpic5_new_two.getText().toString().trim());
        myBean.set("casecarId", et_accdentidcardme_new_two.getText().toString().trim().toUpperCase());//驾驶证号
        myBean.set("casecarphone", et_accidentloadingpictwo_new_two.getText().toString().trim());//联系电话
        myBean.set("accidentType", accidentType);
        myBean.set("discription", discription);
        myBean.set("appCaseNo", appCaseNo);
        myBean.set("carType", bfCarType);
        myBean.set("frameno", et_accidentcarnum2_new_two.getText().toString().trim().toUpperCase());
        EntityBean[] disputeBean = new EntityBean[]{hisBean, myBean};
        DisputeView disputeview = new DisputeView(context, disputeBean,4);
        FrameworkManager.getInstance().showNewForm(context, disputeview);
    }
    /**
     * 判断输入框是否为空
     *
     */
    private boolean ifEdittextNull() {
        String et_accidentloadingpic1_new_twocontent = et_accidentloadingpic1_new_two.getText().toString().trim();
        String et_accidentloadingpic3_new_twocontent = et_accidentloadingpic3_new_two.getText().toString().trim();
        String et_accidentloadingpic5_new_twocontent = et_accidentloadingpic5_new_two.getText().toString().trim();
        String et_accidentloadingpictwo_new_twocontent = et_accidentloadingpictwo_new_two.getText().toString().trim();
        String et_accidentcarnum1_new_twocontent = et_accidentcarnum1_new_two.getText().toString().trim();//甲方车架号
        String et_accidentcarnum2_new_twocontent = et_accidentcarnum2_new_two.getText().toString().trim();//乙方车架号
        String et_accdentidcardhe_new_twocontent = et_accdentidcardhe_new_two.getText().toString().trim();
        String et_accdentidcardme_new_twocontent = et_accdentidcardme_new_two.getText().toString().trim();
        if (!TextUtils.isEmpty(et_accidentloadingpic1_new_twocontent)
                && !TextUtils.isEmpty(dfCarType)&& !TextUtils.isEmpty(bfCarType)
                && !TextUtils.isEmpty(et_accidentloadingpic5_new_twocontent) && !TextUtils.isEmpty(et_accdentidcardhe_new_twocontent)
                && !TextUtils.isEmpty(et_accdentidcardme_new_twocontent) && !TextUtils.isEmpty(et_accidentcarnum1_new_twocontent)
                && !TextUtils.isEmpty(et_accidentcarnum2_new_twocontent)&& !TextUtils.isEmpty(et_accidentloadingpictwo_new_twocontent)) {
            return true;
        } else if (TextUtils.isEmpty(dfCarType)) {
            UiUtil.showToast(context, "请甲方选择车辆类型");
            return false;
        } else if (TextUtils.isEmpty(et_accidentloadingpic1_new_twocontent)) {
            UiUtil.showToast(context, "甲方姓名为空");
            return false;
        } else if (TextUtils.isEmpty(et_accidentcarnum1_new_twocontent)) {
            UiUtil.showToast(context, "甲方车辆品牌为空");
            return false;
        } else if (TextUtils.isEmpty(et_accidentloadingpic3_new_twocontent)) {
            UiUtil.showToast(context, "甲方联系电话为空");
            return false;
        } else if (TextUtils.isEmpty(et_accdentidcardhe_new_twocontent)) {
            UiUtil.showToast(context, "甲方身份证号为空");
            return false;
        } else if (TextUtils.isEmpty(bfCarType)) {
            UiUtil.showToast(context, "请乙方选择车辆类型");
            return false;
        }else if (TextUtils.isEmpty(et_accidentloadingpic5_new_twocontent)) {
            UiUtil.showToast(context, "乙方姓名为空");
            return false;
        }  else if (TextUtils.isEmpty(et_accidentcarnum2_new_twocontent)) {
            UiUtil.showToast(context, "乙方车辆品牌为空");
            return false;
        }else if (TextUtils.isEmpty(et_accidentloadingpictwo_new_twocontent)) {
            UiUtil.showToast(context, "乙方联系电话为空");
            return false;
        }else if (TextUtils.isEmpty(et_accdentidcardme_new_twocontent)) {
            UiUtil.showToast(context, "乙方身份证号为空");
            return false;
        }
        return false;
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            showHomeView();
        }
        return false;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        myHandler=null;
        regEvent(false);
        closeForm();
    }

    /**
     * 提示是否不继续处理，返回首页
     */
    private void showHomeView() {
        UiUtils.showDialog(context, null, null, "是否放弃输入，结束本次快处并返回首页", "返回", "确认",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        closeAllForm();
                        OnDestroy();
                        HomePageView homepageview = new HomePageView(context);
                        FrameworkManager.getInstance().showNewForm(context, homepageview);
                    }
                });
    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (view == null) {
                return;
            }
            if (msg.what == 0004) {
                if (null != nationid && "1".equals(nationid)) {
                    et_wz1.setText(imageaddress);
                }
                //初始化信息，从拍照界面返的信息
                EntityBean jfJszBean = mApplication.getJfJszBean_new();//甲方驾驶证
                if(null!=jfJszBean){
                    String ocrname = jfJszBean.getString("name");//姓名
                    String ocrcardno = jfJszBean.getString("cardno");//驾驶证号
                    if(null!=ocrname){
                        et_accidentloadingpic1_new_two.setText(ocrname);
                    }
                    if(null!=ocrcardno){
                        et_accdentidcardhe_new_two.setText(ocrcardno);
                    }
                }
                EntityBean jfXszBean = mApplication.getJfXszBean_new();//甲方行驶证
                if(null!=jfXszBean){
                    String ocrvin = jfXszBean.getString("ocrvin");//车架号
                    if(null!=ocrvin){
                        et_accidentcarnum1_new_two.setText(ocrvin);
                    }
                }
                EntityBean yfJszBean = mApplication.getYfJszBean_new();//乙方驾驶证
                if(null!=yfJszBean){
                    String ocrname = yfJszBean.getString("name");//姓名
                    String ocrcardno = yfJszBean.getString("cardno");//驾驶证号
                    if(null!=ocrname){
                        et_accidentloadingpic5_new_two.setText(ocrname);
                    }
                    if(null!=ocrcardno){
                        et_accdentidcardme_new_two.setText(ocrcardno);
                    }
                }
                EntityBean yfXszBean = mApplication.getYfXszBean_new();//乙方行驶证
                if(null!=yfXszBean){
                    String ocrvin = yfXszBean.getString("ocrvin");//车架号
                    if(null!=ocrvin){
                        et_accidentcarnum2_new_two.setText(ocrvin);
                    }
                }
            }
        }
    };

    // 调取照片识别的接口
    private void pictureRecognitionData() {
        try {
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
            EntityBean bean = new EntityBean();
            String userName = mApplication.getLoginInfo().getBean("userinfo")
                    .getString("userflag");
            bean.set("appcaseno", appCaseNo);
            bean.set("userflag", userName);
            bean.set("token", mApplication.getToken());
            LoadDataManagerCar.getInstance(context).callService(null,
                    mApplication.getServerUrl(), URLConstant.JJAPPGETOCRDATA, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                            EntityBean bean = (EntityBean) result;
                            String restate = bean.getString("restate");
                            String redes = bean.getString("redes");
                            imageaddress = bean.getString("imageaddress");
                            EntityBean[] cardata = bean.getBeans("data");
                            if ("1".equals(restate)) {
                                if(null!=cardata){
                                    for (EntityBean element:cardata) {
                                        String imagetype =  element.getString("imagetype");
                                        if("16".equals(imagetype)){//甲驾驶证
                                            mApplication.setJfJszBean_new(element);
                                        }else if("17".equals(imagetype)){//甲方行驶证
                                            mApplication.setJfXszBean_new(element);
                                        }else if("18".equals(imagetype)){//乙方驾驶证
                                            mApplication.setYfJszBean_new(element);
                                        }else if("19".equals(imagetype)){//乙方行驶证
                                            mApplication.setYfXszBean_new(element);
                                        }
                                    }
                                }
                                if (myHandler != null) {
                                    myHandler.sendEmptyMessage(0004);
                                }
                            } else {
                                UiUtil.showToast(context, redes);
                            }
                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                        }
                    });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}
